//
//  LQRefreshView.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/6/16.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit

//UI动画处理
class LQRefreshView: UIView {
    
    //刷新状态
    var refreshStatus:LQRefreshStatus = .Normal
    
    //指示器
    @IBOutlet weak var tipIcon: UIImageView!

    //指示文本
    @IBOutlet weak var tipLabel: UILabel!
    
    //提示标签
    @IBOutlet weak var Indicator: UIActivityIndicatorView!
    
    class func refrshView()->LQRefreshView{
        
        let nib = UINib.init(nibName: "LQRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! LQRefreshView
    }
    
    func setupRefreshStatus(status:LQRefreshStatus) {
        
        refreshStatus = status
        
        /*
         ios 内 默认顺时针旋转
         就近原则（不到180 ，如179.009，就会判断“原路返回”要比“顺时针”返回路要近，就会实现，顺时针旋转，逆时针返回）
         要实现360，需要核心动画，CABaseAnimation
         */
        switch status {
        case .Normal:
            tipLabel.text = "继续使劲拉..."
            tipIcon.isHidden = false
            
            Indicator.stopAnimating()
            
            UIView.animate(withDuration: 0.25, animations: {
                
                self.tipIcon.transform = .identity
            })
        case .Pull:
            tipLabel.text = "放手就刷新..."
            UIView.animate(withDuration: 0.25, animations: { 
                
                self.tipIcon.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.001))//M_PI 180度
            })
            
        case .WillRefresh:
            tipLabel.text = "正在刷新中..."
            
            tipIcon.isHidden = true
            
            Indicator.startAnimating()
        }
        
        
    }

}
